//
//  KLSecondChildKeyTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/8/26.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLSecondChildKeyTableViewController.h"
#import "KLHttpAPI.h"
#import "KLHelp.h"
#import "HemaConst.h"
#import "JRToast.h"
#import "Masonry.h"
#import "HemaFunction.h"
#import "JRToast.h"

@interface KLSecondChildKeyTableViewController ()
{
    NSArray *childKeys;
}
@end

@implementation KLSecondChildKeyTableViewController
-(void)viewWillAppear:(BOOL)animated{
    [self refreshSecondChildKey];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self naveSet];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return childKeys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondChildKey"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondChildKey"];
    }else{
        //删除cell的所有子视图
        while ([cell.contentView.subviews lastObject] != nil){
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    NSDictionary *lockDic = childKeys[indexPath.row];
    NSString *valid_type = [lockDic objectForKey:@"valid_type"];
    if(valid_type.integerValue == 1){
        UILabel *notice = [[UILabel alloc]init];
        notice.text = @"未生效";
        notice.numberOfLines = 0;
        notice.font = [UIFont systemFontOfSize:16];
        notice.textAlignment = NSTextAlignmentCenter;
        notice.textColor = RGB_UI_COLOR(238, 230, 133);
        [cell.contentView addSubview:notice];
        [notice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.centerY.equalTo(cell.contentView).with.offset(0);
            make.width.mas_equalTo(17);
        }];
    }else if(valid_type.integerValue == 2){
        UILabel *notice = [[UILabel alloc]init];
        notice.text = @"已过期";
        notice.numberOfLines = 0;
        notice.font = [UIFont systemFontOfSize:16];
        notice.textAlignment = NSTextAlignmentCenter;
        notice.textColor = RGB_UI_COLOR(240, 101, 101);
        [cell.contentView addSubview:notice];
        [notice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.centerY.equalTo(cell.contentView).with.offset(0);
            make.width.mas_equalTo(17);
        }];
    }else if (valid_type.integerValue == 4){
        UILabel *notice = [[UILabel alloc]init];
        notice.text = @"可使用";
        notice.numberOfLines = 0;
        notice.font = [UIFont systemFontOfSize:16];
        notice.textAlignment = NSTextAlignmentCenter;
        notice.textColor = RGB_UI_COLOR(0, 205, 102);
        [cell.contentView addSubview:notice];
        [notice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.centerY.equalTo(cell.contentView).with.offset(0);
            make.width.mas_equalTo(17);
        }];
    }
    
    UILabel *lockMasterName = [[UILabel alloc]init];
    if([[lockDic objectForKey:@"type"] isEqualToString:@"1"]){
        lockMasterName.text = @"平等钥匙";
    }else if([[lockDic objectForKey:@"type"] isEqualToString:@"2"]){
        lockMasterName.text = @"临时钥匙";
    }
    [cell.contentView addSubview:lockMasterName];
    [lockMasterName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell).with.offset(5);
        make.height.mas_equalTo(25);
        make.left.equalTo(cell).with.offset(45);
    }];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = [lockDic objectForKey:@"name"];
    nameLabel.textAlignment = NSTextAlignmentRight;
    nameLabel.textColor = [UIColor lightGrayColor];
    nameLabel.alpha = 0.8;
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.numberOfLines = 1;
    nameLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [cell.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lockMasterName).with.offset(0);
        make.right.equalTo(cell).with.offset(-40);
        make.left.equalTo(lockMasterName).with.offset(80);
    }];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.alpha = 0.8;
    timeLabel.font = [UIFont systemFontOfSize:13];
    NSString *starTime = [lockDic objectForKey:@"startdate"];
    starTime = [starTime substringToIndex:16];
    NSString *endTime = [lockDic objectForKey:@"enddate"];
    endTime = [endTime substringToIndex:16];
    timeLabel.text = [NSString stringWithFormat:@"有效期:%@至%@",starTime,endTime];
    
    [cell.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell).with.offset(30);
        make.height.mas_equalTo(25);
        make.left.equalTo(cell).with.offset(45);
    }];
    
    UILabel *phoneLabel = [[UILabel alloc]init];
    phoneLabel.textColor = [UIColor lightGrayColor];
    phoneLabel.alpha = 0.8;
    phoneLabel.font = [UIFont systemFontOfSize:13];
    //发出的钥匙
    phoneLabel.text = [NSString stringWithFormat:@"此钥匙下发至用户:%@ （%@）",[lockDic objectForKey:@"recevier_phone"],[lockDic objectForKey:@"recevier_username"]];
    
    [cell.contentView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell).with.offset(50);
        make.height.mas_equalTo(25);
        make.left.equalTo(cell).with.offset(45);
    }];
    
    UILabel *address = [[UILabel alloc]init];
    address.textColor = [UIColor lightGrayColor];
    address.numberOfLines = 1;
    address.lineBreakMode = NSLineBreakByTruncatingMiddle;
    address.alpha = 0.8;
    address.font = [UIFont systemFontOfSize:13];
    address.text = [NSString stringWithFormat:@"地址:%@",[lockDic objectForKey:@"address"]];
    [cell.contentView addSubview:address];
    [address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell).with.offset(70);
        make.height.mas_equalTo(25);
        make.left.equalTo(cell).with.offset(45);
        make.right.equalTo(cell).with.offset(-15);
    }];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95;
}

#pragma mark -界面设置
//初始化导航栏
-(void)naveSet{
    self.title = @"授权用户下发子钥匙";
    self.view.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 刷新二级锁主子钥匙列表
-(void)refreshSecondChildKey{
    [KLHelp showHUDAddedTo:self.view animated:YES];
    [KLHttpAPI requestSecondChildKeyWithSecondID:[self.secondLockInfor objectForKey:@"id"] lock_id:[self.lockInfor objectForKey:@"id"] completionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"二级锁主子钥匙列表:%@",operation.responseJSON);
        NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            [KLHelp hideAllHUDsForView:self.view animated:YES];
            NSArray *infor = [operation.responseJSON objectForKey:@"infor"];
            if(![infor isKindOfClass:[NSNull class]] && infor.count != 0){
                childKeys = [NSArray arrayWithArray:infor];
                [self.tableView reloadData];
            }else{
                [KLHelp setNoDateImageWithView:self.view type:0];
            }
        }
    } errorHandler:^(NSString *error) {
        [KLHelp setNoDateImageWithView:self.view type:1];
    }];
}

@end
